﻿var css_HIDE="FONT-SIZE=10pt;CURSOR:hand;PADDING-TOP:2px;HEIGHT:22px;";
var css_Span,css_Filter;
var calendarStyle;
var InitDate=new DateInfo();
var calendarContainer;
var showTime=false;

//��ȡ����
function getDateFromText(container)
{
	calendarContainer=container;
	var year,month,day,hour,minute
	hour="";
	minute="";	
	year=calendarContainer.rows[0].cells[0].children[0].value
	month=calendarContainer.rows[0].cells[0].children[1].value
	day=calendarContainer.rows[0].cells[0].children[2].value
	if(calendarContainer.rows[0].cells[0].children[3]!=undefined)
	{
		showTime=true
		hour=calendarContainer.rows[0].cells[0].children[3].value
	}
	else
		showTime=false;
	if(calendarContainer.rows[0].cells[0].children[4]!=undefined)
	minute=calendarContainer.rows[0].cells[0].children[4].value
	if(isDate(year+"-"+month+"-"+day))
	{
		InitDate.Year=year;
		InitDate.Month=month;
		InitDate.Day=day;
		if(hour.length==0)
			hour=0;
		InitDate.Hour=hour;
		if(minute.length==0)
			minute=0;
		InitDate.Minute=minute;
	}
}
function PadLeft(str,length,padChar)
{
	var i;
	if(str.length<length)
	{
		var tempstr="";
		for(i=0;i<length-str.length;i++)
		{
			tempstr+=padChar;			
		}
		return tempstr+str;
	}
	return str;
}
function getSelectDate(selectdate)
{
	if(!validateDate(selectdate))
	{
		alert("無效的日期格式")
		return;
	}
	calendarContainer.rows[0].cells[0].children[0].value=selectdate.Year
	calendarContainer.rows[0].cells[0].children[1].value=PadLeft(selectdate.Month,2,'0')
	calendarContainer.rows[0].cells[0].children[2].value=PadLeft(selectdate.Day,2,'0')
	if(calendarContainer.rows[0].cells[0].children[3]!=undefined)
		calendarContainer.rows[0].cells[0].children[3].value=PadLeft(selectdate.Hour,2,'0')
	if(calendarContainer.rows[0].cells[0].children[4]!=undefined)
		calendarContainer.rows[0].cells[0].children[4].value=PadLeft(selectdate.Minute,2,'0');
	calendarContainer.parentElement.children[1].value=selectdate.Year+"-"+selectdate.Month+"-"+selectdate.Day+" "+selectdate.Hour+":"+selectdate.Minute;
	if(calendarContainer.getAttribute("AutoPostBack")=="True")
		PostBack(calendarContainer.parentElement.id);
}
function OnDateChanged()
{
	var year,month,day,hour,minute
	calendarContainer=window.event.srcElement.parentElement.parentElement.parentElement.parentElement;
	var yearControl = calendarContainer.rows[0].cells[0].children[0];
	year=Trim(yearControl.value)
	month=Trim(calendarContainer.rows[0].cells[0].children[1].value)
	day=Trim(calendarContainer.rows[0].cells[0].children[2].value)
	if(calendarContainer.rows[0].cells[0].children[3]!=undefined)
	{
		showTime=true;
		hour=Trim(calendarContainer.rows[0].cells[0].children[3].value)
	}
	else
		showTime=false;
	if(calendarContainer.rows[0].cells[0].children[4]!=undefined)
		minute=Trim(calendarContainer.rows[0].cells[0].children[4].value)
	if(year.length==0 || month.length==0 || day.length==0)
		return;
	if(showTime)
	{
		if(hour.length==0 || minute.length==0)
			return;
	}
	//��֤�����Ƿ���Ч
	var di=new DateInfo();
	di.Year=year;
	di.Month=month;
	di.Day=day;
	di.Hour=hour;
	di.Minute=minute;
	if(!validateDate(di))
	{
		alert("無效的日期格式");
		yearControl.focus();
		yearControl.select();
		return;
	}
	else if(year < 1900)//mantis
	{
		alert("填寫的日期不能在1900年以前");
		yearControl.focus();
		yearControl.select();
		return
	}
	calendarContainer.parentElement.children[1].value=year+"-"+month+"-"+day+" ";
	if(showTime)
		calendarContainer.parentElement.children[1].value+=hour+":"+minute;
	if(calendarContainer.getAttribute("AutoPostBack")=="True")
		PostBack(calendarContainer.parentElement.id);
}
function Trim(s)
{
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}
function popCalendar(obj,colorStyle)
{	
	getDateFromText(obj);
	if (window.Calendar==undefined) Create();
	if(showTime)
	{
		//Time
		if(Calendar.TT.rows.length==8)
		{
			Calendar.height+=15;
			var timerow=Calendar.TT.insertRow();
			var timetd=timerow.insertCell();
			timetd.colSpan=7;
			timetd.align="right"
			timetd.style.backgroundColor="white";
			timetd.innerHTML="時間(24小時制)&nbsp;&nbsp;<input type=text maxlength=2 style='width:30px;height:18px;'>時&nbsp;<input type=text maxlength=2 style='width:30px;height:18px;'>分&nbsp;<font size=2 onclick='javascript:returnTime()' style='cursor:hand'><b>確定</b></font>";
		}
	}
	else
	{
		if(Calendar.TT.rows.length>8)
		{
			Calendar.height=170;			
			Calendar.TT.deleteRow()
		}
	}
	Calendar.style.cssText="Z-INDEX:2000;display='inline';position: absolute;width: 310px;height: "+Calendar.height+"px;";
	setColor(colorStyle);
	Write(InitDate.Year,InitDate.Month);
	setCalendarPosition(obj);
	return false;
}
 //set position
 function setCalendarPosition(e)

		{
 		var t=e.offsetTop+e.offsetHeight;
		var l=e.offsetLeft;
		var obj=e;
		while(e=e.offsetParent)
		{
		t+=e.offsetTop;
		l+=e.offsetLeft;
		}
		if (l>(document.body.offsetWidth-window.Calendar.width+document.body.scrollLeft)&&(l-window.Calendar.width+obj.offsetWidth+22-document.body.scrollLeft)>=0)
		{
			l=document.body.offsetWidth-Calendar.width-obj.rows[0].cells[1].offsetWidth-document.body.rightMargin
		}
		if (t>(document.body.offsetHeight-Calendar.height+document.body.scrollTop)&&(t-window.Calendar.height-obj.offsetHeight-document.body.scrollTop)>=0)
		{
			t-=(Calendar.height+obj.offsetHeight);
		}
		else
		{
			t+=5;
		}
		Calendar.style.top=t;
		Calendar.style.left=l;
		Calendar.style.display="inline"
		Calendar.focus();
//		Calendar.onblur=function(){if(document.activeElement!=this) Calendar.style.display='none'; else this.focus()}*/
		}
	
//set Color Style
function setColor(style)
{
	calendarStyle=style;
	Calendar.Hweek.bgColor=style.WeekBackColor;
	Calendar.TT.borderColor=style.GridColor;
	Calendar.border.style.borderColor=style.BorderColor;
	css_Span="<SPAN style='WIDTH:100%;HEIGHT=100%;FONT-SIZE:10pt;FONT-WEIGHT: bold;COLOR:"+style.HightColor+";BORDER-COLOR: "+style.HightColor+";BORDER-WIDTH: 2px;BORDER-STYLE: dotted; POSITION: static'>";
	css_Filter="progid:dximagetransform.microsoft.gradient(gradienttype=0, StartColorstr="+style.StartColor+", EndColorstr="+style.EndColor+");";
	Calendar.head.cells[0].style.filter=css_Filter;
	Calendar.head.cells[1].style.filter=css_Filter;
	Calendar.head.cells[2].style.filter=css_Filter;
 }
//Create
function Create()
{
window.Calendar=document.createElement("DIV");
document.body.insertBefore(Calendar);
Calendar.width=310;
Calendar.height=170;
with(Calendar)
{
	borderColor="black";
	style.cssText="Z-INDEX:2000;display='inline';position: absolute;width: 310px;height: "+Calendar.height+"px;";
	innerHTML="<iframe width=100% height=100% scrolling=no frameborder=0></iframe>"
}
//border
Calendar.border=document.createElement("DIV");
Calendar.insertAdjacentElement("AfterBegin",Calendar.border);
with(Calendar.border)
{
	borderColor="red";
	style.cssText="position: absolute;top:0px;left:0px;width:100%;height: 100%;";
	style.borderWidth="1px";
	style.borderStyle="solid";
}
//body
Calendar.TT=document.createElement("Table");
Calendar.border.insertAdjacentElement("AfterBegin",Calendar.TT);
	with(Calendar)
	{	
		TT.style.cssText="FONT-SIZE:9pt;width:100%;height:100%;tableLayout:fixed;position:absolute;top:0px,left:0px;border-width:1px;border-style:solid;border-collapse:collapse";
		TT.border=1;
		TT.cellSpacing="0";
		TT.cellPadding="0";
		//TT.onselectstart=function(){return false}
		//Head
		Calendar.head=TT.insertRow();
		myTr=Calendar.head;
		myTr.style.height="25px";
		myTd=myTr.insertCell();
		myTd.align="center";
		myTd.style.fontFamily="webdings";
		myTd.style.borderRightWidth=0;
		myTd.onmouseover=function(){Calendar.onblur=""}
		myTd.onmouseout=function(){setFocus()}
		myTd.style.backgroundColor="red";
		myTd.innerHTML="<Font color='black' style='font-size:9pt;cursor:hand'title='上一年' onclick='javaScript:setYear(-1)'>9</Font>&nbsp;<Font color='black' title='上一月' style='font-size:9pt;cursor:hand' onclick='javaScript:setMonth(-1)'>7</Font>";
		//Center
		myTd=myTr.insertCell();
		myTd.align="center";
		myTd.valign="middle";
		//Select Year
		Calendar.Syear=document.createElement("SPAN");
		Calendar.Syear.onmouseover=function(){this.style.color=calendarStyle.HightColor;this.style.textDecoration="underline"}
		Calendar.Syear.onmouseout=function(){this.style.color="black";this.style.textDecoration=""}
		Calendar.Syear.style.cssText=css_HIDE;
		Calendar.Syear.onclick=function(){this.style.display='none';Calendar.Dyear.style.display="";Calendar.onblur="";Calendar.Dyear.focus()}
		Calendar.Syear.style.width=67;
		myTd.insertBefore(Calendar.Syear);
		Calendar.Dyear=document.createElement("Select")
		for (var i=2058;i>=1949;i--)
		{	
			myop=new Option();
			myop.value=i;
			myop.text=i+"年";
			Calendar.Dyear.options.add(myop);
		}
		Calendar.Dyear.style.display='none';
		Calendar.Dyear.onblur=function(){this.style.display='none';	Calendar.Syear.style.display="";if (inRect(event.x,event.y)==false) Calendar.style.display='none'}
		Calendar.Dyear.onchange=function(){Write(Calendar.Dyear.value,Calendar.Dmonth.value)};
		myTd.insertBefore(Calendar.Dyear);
		//Select Month
		Calendar.Smonth=document.createElement("SPAN");
		Calendar.Smonth.onmouseover=function(){this.style.color=calendarStyle.HightColor;this.style.textDecoration="underline"}
		Calendar.Smonth.onmouseout=function(){this.style.color="black";this.style.textDecoration=""}
		Calendar.Smonth.style.cssText=css_HIDE; 
		Calendar.Smonth.onclick=function(){this.style.display='none';Calendar.Dmonth.style.display="";Calendar.onblur="";Calendar.Dmonth.focus()}
		Calendar.Smonth.style.width=53;
		myTd.insertBefore(Calendar.Smonth);
		Calendar.Dmonth=document.createElement("Select")
		for (var i=1;i<=12;i++)
		{	
			myop=new Option();
			myop.value=PadLeft(i.toString(),2,'0');
			myop.text=i+"月";
			Calendar.Dmonth.options.add(myop);
		}
		Calendar.Dmonth.style.display='none';
		Calendar.Dmonth.onblur=function(){this.style.display='none';Calendar.Smonth.style.display="";if (inRect(event.x,event.y)==false) Calendar.style.display='none'}
		Calendar.Dmonth.onchange=function(){Write(Calendar.Dyear.value,Calendar.Dmonth.value)};
		myTd.insertBefore(Calendar.Dmonth);
		myTd.style.borderLeftWidth=0;
		myTd.style.borderRightWidth=0;
		myTd.style.fontWeight="bold";
		myTd.onmouseover=function(){Calendar.onblur=""}
		myTd.onmouseout=function(){if (document.activeElement!=Calendar.Dyear && document.activeElement!=Calendar.Dmonth) setFocus()}
		myTd.colSpan=5;
		
		myTd=myTr.insertCell();
		myTd.align="center";
		myTd.style.borderLeftWidth=0;
		myTd.style.fontFamily="webdings";
		myTd.onmouseover=function(){Calendar.onblur=""}
		myTd.onmouseout=function(){setFocus()}
		myTd.innerHTML="<Font title='下一月' style='font-size:9pt;cursor:hand' onclick='javaScript:setMonth(1)' color='black'>8</Font>&nbsp<Font  style='font-size:9pt;cursor:hand' color='black' title='下一年'onclick='javaScript:setYear(1)' >:</Font>";
		
		//Week
		var week=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
		Calendar.Hweek=TT.insertRow();
		myTr=Calendar.Hweek;
		for (var i=0;i<=6;i++)
		{
			myTd=myTr.insertCell();
			myTd.align="center";
			myTd.vAling="middle";
			myTd.onmouseover=function(){Calendar.onblur=""}
			myTd.onmouseout=function(){setFocus()}
			myTd.innerText=week[i];
		}
		
		for (var i=1;i<=6;i++)
		{
		myTr=TT.insertRow();
			for (var ii=1;ii<=7;ii++)
				{
					myTd=myTr.insertCell();
					myTd.align="center";
					myTd.vAlign="middle";
					myTd.style.cursor="hand";
					myTd.style.height="20px";
					myTd.onmouseover=function(){this.bgColor=calendarStyle.CurrentColor;Calendar.onblur=""}
					myTd.onmouseout=function(){this.bgColor="white";setFocus()}
					myTd.onclick=function(){_click()};	
				}
		}
		
	}
}
//Set focus
function setFocus()
	{
	if (Calendar.style.display=="" )
		{	
			Calendar.focus();
			//Calendar.onblur=function(){Calendar.style.display='none'}
		}	
	}
//click
function _click()
	{
		var myTd=getParent(event.srcElement,"TD");
		setCalendarStyle(myTd);
		myTd.bgColor="white";
		var temp=getValue(myTd);
		var selectdate=new DateInfo();
		selectdate.Year=temp.split("-")[0];
		selectdate.Month=temp.split("-")[1];
		selectdate.Day=temp.split("-")[2]
		if(showTime)
		{
			selectdate.Hour=Calendar.TT.rows[8].cells[0].children[0].value;
			selectdate.Minute=Calendar.TT.rows[8].cells[0].children[1].value;
		}
		getSelectDate(selectdate);
		Calendar.style.display='none';
	}
function returnTime()
{
	InitDate.Year=Calendar.Dyear.value;
	InitDate.Month=parseInt(Calendar.Dmonth.value,10);
	InitDate.Hour=Calendar.TT.rows[8].cells[0].children[0].value;
	InitDate.Minute=Calendar.TT.rows[8].cells[0].children[1].value;
	getSelectDate(InitDate);
	Calendar.style.display='none';
}
//set Style
function setCalendarStyle(myTd)
	{
	if (Calendar.current!=undefined) 
		{
			with (Calendar.current)
				{	
					if(value.substr(0,1)=="+" || value.substr(0,1)=="-") 
						innerText=value.substr(1); 
					else 
						innerText=value;
				}
		}
		myTd.innerHTML=css_Span+myTd.innerHTML+"</SPAN>";
		Calendar.current=myTd;
	}
//get Value
function getValue(myTd)
	{
	switch(myTd.value.toString().substr(0,1))
		{
			case "-":
				var Da=new Date(Calendar.year,Calendar.month-1 ,1);
				Da.setDate(-0);
				return Da.getFullYear()+"-"+(Da.getMonth()+1)+"-"+myTd.value.substr(1);
			break;
			case "+" :
				var Da=new Date(Calendar.year,Calendar.month-1,1);
				Da.setDate(+32);
				return Da.getFullYear()+"-"+(Da.getMonth()+1)+"-"+myTd.value.substr(1);
			break;
			default:
				return Calendar.year+"-"+Calendar.month+"-"+myTd.value;
					
		}	
	}
//next year pre year
function setYear(N)
{	
	if (Calendar.year+N<1000 || Calendar.year+N>9999){return}
	Write(Calendar.year+N,Calendar.month);	
}
//next month pre month
function setMonth(N)
{	var Da=new Date(Calendar.year,Calendar.month-1,1);
	if (N==-1) Da.setDate(-0);else Da.setDate(+32);
	Write(Da.getFullYear(),Da.getMonth()+1);
}
//fill cell day
function Write(year,month)
{	
	var Da=new Date(year,month-1,1);
	var N=Da.getDay();
	var myTT=Calendar.TT;
	var D=1;
	var fColor="black";
	var sN="";//記錄值 來區分當前月還是上一個月
	Calendar.year=parseInt(year,10);
	Calendar.month=parseInt(month,10);
	Calendar.Dyear.value=parseInt(year,10);
	Calendar.Syear.innerText=year+" 年";
	Calendar.Smonth.innerText=PadLeft(month.toString(),2,'0')+" 月";
	Calendar.Dmonth.value=month;
	for (var i=2;i<=7;i++)
	{
		for (var ii=N;ii<=6;ii++)
		{
			myTT.rows[i].cells[ii].value=sN+D;
			//�Ƿ�Ϊ����
			V=getValue(myTT.rows[i].cells[ii]);
			if (V==getDate(new Date()))
					myTT.rows[i].cells[ii].style.color="red";
			else
					myTT.rows[i].cells[ii].style.color=fColor;
			//�Ƿ�Ϊ��ǰʱ��
			if (V==InitDate.Year+"-"+parseInt(InitDate.Month,10)+"-"+parseInt(InitDate.Day,10))
				{
					myTT.rows[i].cells[ii].innerHTML=css_Span+D+"</SPAN>";
					Calendar.current=myTT.rows[i].cells[ii];
				}
			else
				myTT.rows[i].cells[ii].innerText=D;
			
			if (D==getLastDay(year,month)) {D=1;fColor="Gray";sN="+"} else D++;
		}
	N=0;
	}
	//��һ�����ڱ�ҳ��ʾ�ļ���
	N=Da.getDay()-1;
	Da.setDate(-0);
	year=Da.getYear();
	month=Da.getMonth();
	for (var i=N;i>=0;i--)
		{	D=(getLastDay(year,month+1)+(i-N))
			myTT.rows[2].cells[i].value="-"+D
				//�Ƿ�Ϊ����
			V=getValue(myTT.rows[2].cells[i]);
			if (V==getDate(new Date()))
					myTT.rows[2].cells[i].style.color="red";
			else
					myTT.rows[2].cells[i].style.color=fColor;
			//�Ƿ�Ϊ��ǰʱ��
			if (V==InitDate.Year+"-"+InitDate.Month+"-"+InitDate.Day)
				{	
					myTT.rows[2].cells[i].innerHTML=css_Span+D+"</SPAN>";
					Calendar.current=myTT.rows[2].cells[i];
				}
			else
				myTT.rows[2].cells[i].innerText=D;
		}
	if(showTime)
	{
		myTT.rows[8].cells[0].children[0].value=InitDate.Hour;
		myTT.rows[8].cells[0].children[1].value=InitDate.Minute;
	}
}
// get month last Day
function getLastDay(year,month)
{	var Da=new Date(year,month,1);
	Da.setDate(-0);
	return Da.getDate();
	
}
//get parent
function getParent(myElement,myTagName){
	if (myElement.tagName==myTagName){
			return myElement;
		}
	else{return getParent(myElement.parentElement,myTagName)}
}
//get date string
function getDate(date)
{	var S=date.getFullYear();
	 S+="-"+(date.getMonth()+1).toString();
	 S+="-"+date.getDate();
	return S;
}
//date data validate
function isDate(dateVar) {
    function GetFullYear(year) {
        return (year + parseInt(2000)) - ((year < 2029) ? 0 : 100);
    }
    var  m;
    var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$");
        m = dateVar.match(yearFirstExp);
        var day, month, year;
        if (m != null) {
            day = m[6];
            month = m[5];
            year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
        }
        else {
              return false;          
        }
        month -= 1;
        var date = new Date(year, month, day);
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? true : false;
  }
 //OBJ value is Date
 function validateDate(date)
 {
	if(showTime)
	{
		if(isNaN(parseInt(date.Hour,10)) || isNaN(parseInt(date.Minute,10)))
			return false;
	}
	if(date.Hour>23 || date.Hour<0 || date.Minute>59 || date.Minute<0)
		return false;
	return isDate(date.Year+"-"+date.Month+"-"+date.Day)
}
	
//a point in the Calendar rect
function inRect(x,y)
{
	var rect=Calendar.getBoundingClientRect();
	var rect1=Calendar.TT.getBoundingClientRect();		//表格的區域
//	alert("left:"+rect.left+";right:"+rect.right+";top:"+rect.top+";bottom:"+rect1.bottom)
//	alert("x:"+x+";y:"+y)
	//x+=2;
	//y-=15;
	if (x>=rect.left && x<=rect.right && y>=rect.top && y<=rect1.bottom)
	{
	return true;
	}
	else
	{
	return false;
	}

}

function document.onclick()
{
	if(window.Calendar==null || window.Calendar==undefined)
		return;
	if(Calendar.style.display!="inline")
		return;
	else
	{
		if(!inRect(event.x,event.y) && window.event.srcElement.tagName!="SELECT")
			Calendar.style.display="none"
	}
}

/*--------------------------------------------------------------------------------------------------------------------------------------------------*/
//Calendar��ʽ��StartColor='#9DBCEA';EndColor='White';HightColor='#0066FF';CurrentColor='#E9FF33';WeekBackColor='#D8E4F7';GridColor='#9DBCEA';BorderColor='#D0DFFF'
function CalendarStyle()
{
	this.StartColor="#9DBCEA"
	this.EndColor="White"
	this.HightColor="#0066FF"
	this.CurrentColor="#E9FF33"
	this.WeekBackColor="#D8E4F7"
	this.GridColor="#9DBCEA"
	this.BorderColor="#D0DFFF"
}

/*------------------------------------------------------------------------------------------------------------------------------------------------*/
function DateInfo()
{
	var date=new Date();
	this.Year=date.getFullYear()
	this.Month=date.getMonth()+1;
	this.Day=date.getDate();
	this.Hour=0;
	this.Minute=0;
}
